package com.recharge.base;

import com.recharge.wk.domain.TradeMerchant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;

/**
 * Created by qi.cao on 2016/12/1.
 */
public class AuthorityTage extends BodyTagSupport{

    private Integer role;

    @Override
    public int doStartTag() throws JspException {

        if (this.pageContext.getSession().getAttribute("LOGIN_KEY")==null){
            try {
                this.pageContext.getResponse().getWriter().println("<script language=\"javascript\" type=\"text/javascript\"> \n" +
                        "window.location.href=\"403.jsp\"</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return SKIP_BODY;
        }
        TradeMerchant merchantBean = (TradeMerchant) this.pageContext.getSession().getAttribute("LOGIN_KEY");

        // 获取session中用户的rights
        if (role <= merchantBean.getRole()){
            // 显示标签间的文字
            return EVAL_BODY_INCLUDE;
        }
        // 不显示标签间的文字
        return SKIP_BODY;
    }

    public Integer getRole() {
        return role;
    }

    public void setRole(Integer role) {
        this.role = role;
    }
}
